{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "2070c976-eae4-49f4-9817-0fdb868457fa",
   "metadata": {},
   "source": [
    "Chapter 21\n",
    "# 用列表构造多层列标签\n",
    "Book_1《编程不难》 | 鸢尾花书：从加减乘除到机器学习  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "4fed82da-2253-42a0-ab85-513826b8e7d5",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "61811338-354a-4c60-a28e-d355c02ff34f",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 示例数据\n",
    "data = np.random.randint(0,9,size=(8,4))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "adfbbe04-c7f8-4c5b-9cd9-0f0adc1497b5",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 创建两层列标签列表\n",
    "col_arrays = [['A',  'A',  'B',  'B'], \n",
    "              ['X1', 'X2', 'X3', 'X4']]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "4eaf7b30-accf-4b2c-9bba-9002838ccfef",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 创建两层列索引\n",
    "multi_col = pd.MultiIndex.from_arrays(col_arrays, \n",
    "                                      names=['I','II'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "7cfd20bb-9885-4ca1-9c31-3e0e0b0d6440",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th>I</th>\n",
       "      <th colspan=\"2\" halign=\"left\">A</th>\n",
       "      <th colspan=\"2\" halign=\"left\">B</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>II</th>\n",
       "      <th>X1</th>\n",
       "      <th>X2</th>\n",
       "      <th>X3</th>\n",
       "      <th>X4</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>8</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>3</td>\n",
       "      <td>8</td>\n",
       "      <td>8</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>6</td>\n",
       "      <td>2</td>\n",
       "      <td>7</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1</td>\n",
       "      <td>6</td>\n",
       "      <td>4</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>4</td>\n",
       "      <td>6</td>\n",
       "      <td>4</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>7</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "I   A     B   \n",
       "II X1 X2 X3 X4\n",
       "0   0  3  8  6\n",
       "1   3  8  8  0\n",
       "2   6  2  7  2\n",
       "3   1  6  4  7\n",
       "4   5  4  0  5\n",
       "5   3  1  1  0\n",
       "6   4  6  4  3\n",
       "7   5  1  7  6"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 创建DataFrame\n",
    "df = pd.DataFrame(data, columns=multi_col)\n",
    "df"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
